package com.jlastu.dlxy_androidapp.view.main;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;


import com.jlastu.dlxy_androidapp.R;
import com.jlastu.dlxy_androidapp.adapter.GoodsListAdapter;
import com.jlastu.dlxy_androidapp.base.BaseFragment;
import com.jlastu.dlxy_androidapp.contract.MainContract;
import com.jlastu.dlxy_androidapp.entity.Goods;
import com.jlastu.dlxy_androidapp.entity.Photo;
import com.jlastu.dlxy_androidapp.presenter.MainPresenter;
import com.jlastu.dlxy_androidapp.utils.SpUtils;
import com.jlastu.dlxy_androidapp.view.details.CommodityDetails;
import com.jlastu.dlxy_androidapp.view.sell.SellActivity;

import java.util.ArrayList;
import java.util.List;

public class FirstFragment extends BaseFragment implements MainContract.IMainView, View.OnClickListener {

    private RecyclerView mGoodsRecyclerView;
    private EditText mSearchEdit;
    private GoodsListAdapter mRecyclerAdapter;
    private MainContract.IMainPresenter mMainPresenter;
    private List<Goods> mGoodsList;

    private List<Photo> mPhotoList;
    private LinearLayout mLinearLayout1;
    private ImageButton mImageButton;

    private Button mSelect_B;

    private TextView mSelect_1;
    private TextView mSelect_2;
    private TextView mSelect_3;
    private TextView mSelect_4;
    private SpUtils mSpUtils;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        mMainPresenter = new MainPresenter(this, getContext());
        mGoodsList = new ArrayList<>();
        mPhotoList = new ArrayList<>();

        mSearchEdit = mView.findViewById(R.id.search_edit);
        mGoodsRecyclerView = mView.findViewById(R.id.rv_goods);
        mLinearLayout1 = mView.findViewById(R.id.search_view);
        mImageButton = mView.findViewById(R.id.search_view_1);
        mSelect_B = mView.findViewById(R.id.select_b);

        StaggeredGridLayoutManager staggeredGridLayoutManager =
                new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        mGoodsRecyclerView.setLayoutManager(staggeredGridLayoutManager);
        mRecyclerAdapter = new GoodsListAdapter(mGoodsList, mPhotoList);
        mGoodsRecyclerView.setAdapter(mRecyclerAdapter);

        mSelect_1 = mView.findViewById(R.id.all);
        mSelect_2 = mView.findViewById(R.id.Vegetables);
        mSelect_3 = mView.findViewById(R.id.MeatEgg);
        mSelect_4 = mView.findViewById(R.id.RiceNoodles);
        mSelect_1.setSelected(true);

        mSpUtils = new SpUtils(mContext);


    }

    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.search_view_1) {
            OnClickSell();
        } else if (id == R.id.rv_goods) {
            OnClickDetails();
        } else if (id == R.id.select_b) {
            OnClickSelect();
        } else if (id == R.id.all) {
            OnClickAll();
        } else if (id == R.id.Vegetables) {
            OnClickVegetables();
        } else if (id == R.id.MeatEgg) {
            OnClickMeatEgg();
        } else if (id == R.id.RiceNoodles) {
            OnClickRiceNoodles();
        }
    }

    private void OnClickSell() {
        Intent intent = new Intent(getActivity(), SellActivity.class);
        startActivity(intent);
    }

    private void OnClickDetails() {
        Intent intent = new Intent(getActivity(), CommodityDetails.class);
        startActivity(intent);

    }

    private void OnClickSelect() {
        String GoodsName = mSearchEdit.getText().toString().trim();
        if (GoodsName.isEmpty()) {
            Toast.makeText(mContext, "请输入商品名称", Toast.LENGTH_SHORT).show();
        } else {
            mSpUtils.putString("goods_name", GoodsName);
            Intent intent = new Intent(mContext, SelectGoodsActivity.class);
            startActivity(intent);
        }
    }

    private void OnClickAll() {
        mSelect_1.setSelected(true);
        mSelect_2.setSelected(false);
        mSelect_3.setSelected(false);
        mSelect_4.setSelected(false);
        onResume();
    }

    private void OnClickVegetables() {
        mSelect_1.setSelected(false);
        mSelect_2.setSelected(true);
        mSelect_3.setSelected(false);
        mSelect_4.setSelected(false);
        String Classify = mSelect_2.getText().toString().trim();
        mMainPresenter.getGoodsList(Classify);
    }

    private void OnClickMeatEgg() {
        mSelect_1.setSelected(false);
        mSelect_2.setSelected(false);
        mSelect_3.setSelected(true);
        mSelect_4.setSelected(false);
        String Classify = mSelect_3.getText().toString().trim();
        mMainPresenter.getGoodsList(Classify);
    }

    private void OnClickRiceNoodles() {
        mSelect_1.setSelected(false);
        mSelect_2.setSelected(false);
        mSelect_3.setSelected(false);
        mSelect_4.setSelected(true);
        String Classify = mSelect_4.getText().toString().trim();
        mMainPresenter.getGoodsList(Classify);

    }

    @Override
    public void onResume() {
        super.onResume();
        mMainPresenter.getGoodsList();

    }

    @Override
    protected void initListener() {
        mImageButton.setOnClickListener(this);
        mSelect_B.setOnClickListener(this);
        mSelect_1.setOnClickListener(this);
        mSelect_2.setOnClickListener(this);
        mSelect_3.setOnClickListener(this);
        mSelect_4.setOnClickListener(this);
    }

    @Override
    public void showGoodsList(List<Goods> goodsList, List<Photo> photoList) {
        mGoodsList = goodsList;
        mPhotoList = photoList;
        Log.d("", "showGoodsList " + mGoodsList.size());
        mRecyclerAdapter.updateData(mGoodsList, mPhotoList);
    }
}
